<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	String user_name = (session
			.getValue("SPRING_SECURITY_LAST_USERNAME")).toString();
%>
<html>
	<head>
		<title>功率因数曲线</title>
		<link id=style href="../styles/theme/blue/frame.css" type="text/css"
			rel="stylesheet" />
		<style type="text/css">
.divLinechart {
	width: 100%;
	margin: 5px;
}
</style>
		<script type='text/javascript' src='../ajax-pushlet-client.js'></script>
		<script type='text/javascript' src='../js/jquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery.blockUI.js'></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/highcharts.js"></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/highChartsTheme.js"></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/scripts/curveCommon.js"></script>
		<script>
function onData(event){

	var powerFactorData = event.get("powerFactorData");
	if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101010d") { // 处理功率因数曲线返回数据
    	$("#tPowerFactorData").val(powerFactorData);
    }
    else if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101020d") {// 处理A相功率因数曲线返回数据
    	$("#aPowerFactorData").val(powerFactorData);
    }
    else if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101040d") {// 处理B相功率因数曲线返回数据
    	$("#bPowerFactorData").val(powerFactorData);
    }
    else if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101080d") {// 处理C相功率因数曲线返回数据
    	$("#cPowerFactorData").val(powerFactorData);
    }
    
    // 如果响应都返回后，根据数据绘制曲线图
    if ($("#tPowerFactorData").val() != "" && $("#aPowerFactorData").val() != "" && $("#bPowerFactorData").val() != "" && $("#cPowerFactorData").val() != "" && $("#tPowerFactorData").val() != undefined  && $("#aPowerFactorData").val() != undefined && $("#bPowerFactorData").val() != undefined && $("#cPowerFactorData").val() != undefined ) {
	    // 准备总、A、B、C相功率因数
	    var tPowerFactorDataArr = $("#tPowerFactorData").val().split(":");
	    var aPowerFactorDataArr = $("#aPowerFactorData").val().split(":");
	    var bPowerFactorDataArr = $("#bPowerFactorData").val().split(":");
	    var cPowerFactorDataArr = $("#cPowerFactorData").val().split(":");
	    var startTime = event.get("startTime");
		
		var tPowerFactorDataArrDouble = new Array();
		var aPowerFactorDataArrDouble = new Array();
		var bPowerFactorDataArrDouble = new Array();
		var cPowerFactorDataArrDouble = new Array();
		for (var i=0; i<aPowerFactorDataArr.length; i++) {
			tPowerFactorDataArrDouble[i] = parseFloat(tPowerFactorDataArr[i]);
			aPowerFactorDataArrDouble[i] = parseFloat(aPowerFactorDataArr[i]);
			bPowerFactorDataArrDouble[i] = parseFloat(bPowerFactorDataArr[i]);
			cPowerFactorDataArrDouble[i] = parseFloat(cPowerFactorDataArr[i]);
		}
		
		// 修正各曲线的最大值
		var tCorrectYAxisMax = correctYAxisMax(tPowerFactorDataArrDouble, 2/3);
		var aCorrectYAxisMax = correctYAxisMax(aPowerFactorDataArrDouble, 2/3);
		var bCorrectYAxisMax = correctYAxisMax(bPowerFactorDataArrDouble, 2/3);
		var cCorrectYAxisMax = correctYAxisMax(cPowerFactorDataArrDouble, 2/3);
		
		var maxVal = -999999999;
		maxVal = tCorrectYAxisMax > maxVal ? tCorrectYAxisMax : maxVal;
		maxVal = aCorrectYAxisMax > maxVal ? aCorrectYAxisMax : maxVal;
		maxVal = bCorrectYAxisMax > maxVal ? bCorrectYAxisMax : maxVal;
		maxVal = cCorrectYAxisMax > maxVal ? cCorrectYAxisMax : maxVal;
		maxVal = maxVal > 0 ? maxVal : 10;
		
		// 修正各曲线的最小值
		var tCorrectYAxisMin = correctYAxisMin(tPowerFactorDataArrDouble, 2/3);
		var aCorrectYAxisMin = correctYAxisMin(aPowerFactorDataArrDouble, 2/3);
		var bCorrectYAxisMin = correctYAxisMin(bPowerFactorDataArrDouble, 2/3);
		var cCorrectYAxisMin = correctYAxisMin(cPowerFactorDataArrDouble, 2/3);
		
		var minVal = 0;
		minVal = tCorrectYAxisMin < minVal ? tCorrectYAxisMin : minVal;
		minVal = aCorrectYAxisMin < minVal ? aCorrectYAxisMin : minVal;
		minVal = bCorrectYAxisMin < minVal ? bCorrectYAxisMin : minVal;
		minVal = cCorrectYAxisMin < minVal ? cCorrectYAxisMin : minVal;
		
		// 准备时间
		var time = new Array();
		time[0] = startTime;
		var date= new Date(Date.parse(startTime.replace(/-/g,   "/"))); 
		for (var i=1; i<aPowerFactorDataArr.length; i++) {
		
			var date2Minsec = date.getTime();
		
			var addFifteen = 1000 * 60 * 15;
			date2Minsec += addFifteen;
			date = new Date(date2Minsec);
			time[i]= date2Str(date);
		}
	
		// 创建折线图，生成后放置在id为container的div中
		titleText = "功率因数曲线";
		colors = fourPhaseColor;
		yAxisText = "功率因数(%)";
		unit = "%";
		seriesVal = [{
			name: '功率因数',
			data: tPowerFactorDataArrDouble
		}, {
			name: 'A相功率因数',
			data: aPowerFactorDataArrDouble
		}, {
			name: 'B相功率因数',
			data: bPowerFactorDataArrDouble
		},{
			name: 'C相功率因数',
			data: cPowerFactorDataArrDouble
		}];
		var container = parent.window.frames["lineChart"].document.getElementById("container");
		var chart = showChart(container, titleText, startTime, colors, time, yAxisText, maxVal, minVal, unit, seriesVal);
    }
}

// pushlet ajax开始，监听/source/powerFactor的报文
PL._init('<%=user_name%>');
PL.joinListen('/source/powerFactor');
</script>
	</head>
	<body style="overflow-y: hidden; overflow-x: hidden">
		<input type="hidden" name="tPowerFactorData" id="tPowerFactorData" />
		<input type="hidden" name="aPowerFactorData" id="aPowerFactorData" />
		<input type="hidden" name="bPowerFactorData" id="bPowerFactorData" />
		<input type="hidden" name="cPowerFactorData" id="cPowerFactorData" />
		<div class="divLinechart" id="container">
		</div>
	</body>
</html>
